/**
 * imgmainthrd.c
 */

#include "avVidMainThrd.h"
#include "avImgMainThrd.h"

/**
 * process one chnvid snapshot
 * @param
 *		pChnvid	input	chnvid pointer
 */
static void procChnVidSS(ChnVid *pChnVid) 
{
	if (pChnVid->pParam->isVideo) 
       {
		ImgEncEnv *pEnv = &pChnVid->imgEncEnv;
		pthread_mutex_lock(&pEnv->lock);
		
		bool schemeSS = (pEnv->pCapturer != NULL && isCurrInSchemeListRange(pEnv->pCapturer->pSchemeList));
		if ((!pEnv->snapshoting) && (pEnv->ssParam.alarmSS || schemeSS)) {
			pEnv->snapshoting = true;
			CreateThread(&pEnv->tid, imgThrd, pChnVid);
		}
		
		pthread_mutex_unlock(&pEnv->lock);
	}
}

void *imgMainThrd(void *arg) 
{
	int i;

	LOGINFO("go to imgMainThrd loop\n");
	while (!g_avEnv.quit) 
	{
		for (i = 0; i < CAPTURER_NUM; i++) 
		{
			procChnVidSS(&g_avEnv.aPriChnVid[i]);
			procChnVidSS(&g_avEnv.aSecChnVid[i]);
		}
		usleep(1000 * 100);	// 100ms
	}

	LOGINFO("imgMainThrd quit\n");
	return NULL;
}

